
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    path: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 核销二维码 (res) =>
   */
  tap_scanCode: function () {
    var that = this;
    wx.scanCode({
      success: (res) => {
        wx.showToast({
          title: '扫码成功,信息查询中!',
        });
        console.log(res);
        var path = res.path;
        that.setData({
          path: path
        });
        app.util.request({
          'url': 'entry/wxapp/Store',
          data: {
            ky_yibao: path
          },
          success(res) {
            console.log(res)
            var rpjson = res.data.data
            console.log(rpjson)
            that.setData({
              order: rpjson
            });
            
            if (rpjson.zy_zhenzhuang == '1') {
              wx.showModal({
                title: '核销失败',
                content: '该订单已核销过,无法再次核销!',
                showCancel: false
              });
              return false;
            }
            else if (rpjson.zy_telephone != path) {
              wx.showModal({
                title: '核销失败',
                content: '该订单不存在!',
                showCancel: false
              });
              return false;
            }
            else {
              wx.showModal({
                title: '提示',
                content: '是否核销该订单',
                confirmText: '确认核销',
                success: function (res) {
                  console.log(res.confirm)
                  if (res.confirm) {
                    app.util.request({
                      'url': 'entry/wxapp/Save_order',
                      data: {
                        oncode: rpjson.zy_telephone,
                        openid: wx.getStorageSync('openid')
                      },
                      success(res) {
                        console.log(res)
                        that.setData({
                          hexiaop: res.data.data
                        })
                        wx.showModal({
                          title: '核销成功',
                          content: '该订单已成功核销!',
                          showCancel: false
                        });

                      }
                    });
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              });

            }
          }
        });


      },
      fail: (res) => {
        //console.log(res);
        wx.showToast({
          image: '../images/error.png',
          title: '扫码失败!',
        });
      }
    });

  }
})